package ronin.fx;

import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import ronin.graphics.*;
import ronin.util.*;
import ronin.fx.*;

public class PSSmokeSwirl extends ParticleSystem
{
	
	public double  dieVal,		// the value of the dying counter 
				dieAmount = 0.0000000005;	// ammount to kill by
	
		public PSSmokeSwirl(Vector2D pos, ParticleManager m)
	{
		super(pos, m);
		
		defInitInfo = new PSInitInfo();
		defInitInfo.type = ParticleSystem.PS_SMOKE_SWIRL;
		defInitInfo.maxParticles = 70;
		defInitInfo.shape = new Rectangle2D.Double(0.0, 0.0, 5.0, 5.0);
		defInitInfo.texture = null;
		defInitInfo.textureFile = "";
		defInitInfo.alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.055f);
		defInitInfo.bounds = new Rectangle2D.Double(0.0, 0.0, 30.0, 30.0);
		
		dieVal = maxParticles;
		forces = new Vector2D.Integer(0, 0);
		immortal = true;
		lifespan = 10;
		setupSystem(defInitInfo);
		startParticle(maxParticles);
	}
	
	
	public PSSmokeSwirl(Vector2D pos, ParticleManager m, int maxLifeSpan)
	{
		super(pos, m);
		
		defInitInfo = new PSInitInfo();
		defInitInfo.type = ParticleSystem.PS_SMOKE_SWIRL;
		defInitInfo.maxParticles = 15;
		defInitInfo.shape = new Rectangle2D.Double(0.0, 0.0, 5.0, 5.0);
		defInitInfo.texture = null;
		defInitInfo.textureFile = "";
		defInitInfo.alpha = AlphaComposite.getInstance(AlphaComposite.SRC_IN, 0.025f);
		defInitInfo.bounds = new Rectangle2D.Double(0.0, 0.0, 30.0, 30.0);
		
		dieVal = maxParticles;
		forces = new Vector2D.Integer(0, 0);
		immortal = false;
		lifespan = maxLifeSpan;
		setupSystem(defInitInfo);
		startParticle(maxParticles);
	}
	
	
	public void dieSlowly(int percentage)
	{
		double r = Math.random()*100;
		
		if(r < percentage)
		{
			maxParticles -= 1;
		}	
	}
	
	
	public void startParticle(int i)
	{
		if(i <= 0) return; // i must be greater than 0
		
		Particle p;
		double v, vx, vy, r;
		int ang, c;
		
		for(int n=0; n < i; n++)
		{
			r	= Math.random();
			v   	=  .5 + (r*1);
			ang 	=  (int)(Math.random()* 360);
			vx  	=  (MagicUtils.cos(ang) *v);
			vy   	=  (MagicUtils.sin(ang) *v);
			
			p = (Particle) manager.particlePool.removeLast();
			
			p.energy = 30 + (int)(r*10);
			p.size	= 1;
						
			p.pos	 = new Vector2D.Double(0.0, 0.0);
			p.pos.setX(pos.getX());
			p.pos.setY(pos.getY());
			
			p.vel    = new Vector2D.Double(0.0, 0.0);
			p.vel.setX(vx);
			p.vel.setY(vy);
			
			c = (int)(r*4);	
			if(c == 0) p.color = Color.gray.darker().darker();
			else if(c == 1) p.color = Color.gray.darker();
			else if(c == 2) p.color = Color.white;
			else if(c == 3) p.color  = Color.gray.brighter();	
			
			particles.add(p);
		}
	}// startParticle
	
	
	
	public void update()
	{
		super.update();
		
		if(immortal == false)
		{
			lifespan -= 1;
		}
		else
		{
			dieSlowly(55);
	    }
		
		if(lifespan <=0)
		{
			releaseParticles();
			alive = false;
		}
		
		double rand;
		
		Particle p;
		
		for(int i=0; i<particles.size(); i++)
		{
			rand = (Math.random());
			
			p =(Particle) particles.get(i);
			
			//p.size++;
			//if(p.size >=3) p.size=1;
			
			p.size = 1+(int)(rand*1);
			
			if(p.energy-- > 0)	// if still alive
			{
				p.pos.translate(p.vel);
				p.vel.translate(forces);	// natural forces affect velocity of particles
				
				if(rand*100 < 10 ) 
				{
					p.color = p.color.brighter();
				}
				else if(rand*100 < 5)
				{
					p.color = p.color.darker();
				}
			    
				//System.out.println("Particle updated");
			}
			else
			{
				//System.out.println("Particle released.");
				releaseParticle(i);
			}
			
		}
		
		startParticle(maxParticles);
	} // update
	
	
	public void paint(Graphics2D g2d)
	{
	//	g2d.setPaint(Color.black);
	//	g2d.fill(bounds);
		g2d.setComposite(alpha);
		
		Particle p;
		
		for(int i=0; i<particles.size(); i++)
		{
			
			p = (Particle) particles.get(i);
			
			g2d.setTransform(new AffineTransform());
			g2d.translate(p.pos.getX(), p.pos.getY());
			g2d.scale(p.size, p.size);
			g2d.setPaint(p.color);
			g2d.fill(shape);
			
			//System.out.println("Painting");
		}	
	}
}